package socket;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.Timer;

public class Client extends JFrame implements ActionListener{

	/**
	 * Data Members
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final int FRAME_X = 550;
	private final int FRAME_Y = 100;
	private final int FRAME_WIDTH = 400;
	private final int FRAME_HEIGHT = 300;
	private JButton sendButton;
	private final int BUTTON_X = 280, BUTTON_Y=50, BUTTON_WIDTH=110, BUTTON_HEIGHT=30;
	private Timer timer;
	private JTextArea text;
	private final int TEXT_X = 10, TEXT_Y=30, TEXT_WIDTH=260, TEXT_HEIGHT=200;
	/*
	 * Network connections
	 */
	private int port = 5555;
	private Socket connection;
	private String server_address = "192.168.1.6";
	/*
	 * Streams for network connections
	 */
	BufferedReader instream;
	PrintWriter outstream;
	String out_message;
	String in_message;
	
	
	
	/**
	 * Constructor
	 * 
	 * @param title
	 */
	public Client(String title){
		setLayout(null);
		setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT);
		setBackground(Color.GRAY);
		
		/*
		 * Add components
		 */
		sendButton = new JButton("Send message");
		sendButton.setBorder(null);
		sendButton.addActionListener( this );
		sendButton.setBounds( BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT );
		add( sendButton );
		
		text = new JTextArea();
		text.setBorder( BorderFactory.createLoweredBevelBorder() );
		text.setBounds( TEXT_X, TEXT_Y, TEXT_WIDTH, TEXT_HEIGHT );
		text.setLineWrap( true );
		add( text );
		
		
		/*
		 * We will use a timer to check for messages every second
		 * 1 sec == 1000 msec
		 * What the code below will do: it will call actionPerformed once second
		 */
		timer = new Timer( 1000, this );
		timer.start();
		
		setTitle(title);
		setVisible( true );
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		/*
		 * Set up network connections
		 */
		try{
			connection = new Socket(server_address, port);
			outstream = new PrintWriter( connection.getOutputStream(), true );
			
			System.out.println(" Connected to " + 
					  connection.getInetAddress() + ":" + connection.getPort() +
					" using local port " + connection.getLocalPort());
			InputStreamReader ins = new InputStreamReader( connection.getInputStream() );
			instream = new BufferedReader( ins );
		} catch(IOException ioe){
			System.out.println("Not able to connect on port " + port);
		}
	}
	
	public void paint(Graphics g){
		super.paint(g);
	}
	
	public void actionPerformed(ActionEvent arg0) {
		/*
		 * User clicks a button:
		 */
		if (arg0.getSource().equals( sendButton )){
			System.out.println("Send button clicked");
			out_message = text.getText();
			if (outstream != null)
				outstream.println( out_message );
		}
		
		/*
		 * Timer went off
		 */
		if (arg0.getSource().equals( timer )){
			System.out.print(".");
			try {
				if (instream != null && instream.ready())
					in_message = instream.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (in_message != null){
				text.append( "Message from server> " +
						in_message + "\n" );
				in_message = null;
			}
		}
	}

	/**
	 * main method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new Client("Test Client");
	}
}